package com.spring.sunflower.common;

import android.content.Intent;
import android.text.TextUtils;

import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseCompatActivity;
import com.spring.sunflower.constant.Constant;
import com.tencent.smtt.export.external.extension.interfaces.IX5WebSettingsExtension;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * Created by Kevin on 2021/7/13<br/>
 * <p>
 * Describe:<br/>
 */
public class WebViewActivity extends BaseCompatActivity {
    private WebView mWebView;

    @Override
    public void initView() {
        Intent intent = getIntent();
        String title = intent.getStringExtra(Constant.KEY_WEB_VIEW_TITLE);
        String url = intent.getStringExtra(Constant.KEY_WEB_VIEW_URL);
        tvTitle.setText(title);

        mWebView = findViewById(R.id.webView);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                view.loadUrl("javascript:(function(){var objs = document.getElementsByTagName('img'); for(var i=0;i<objs.length;i++) {var img = objs[i];  img.style.maxWidth = '100%'; img.style.height = 'auto'; }})()");
            }
        });
        WebSettings webSetting = mWebView.getSettings();
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setDisplayZoomControls(false);
        webSetting.setUseWideViewPort(false);
        webSetting.setSupportMultipleWindows(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setBlockNetworkImage(false);
        webSetting.setLoadWithOverviewMode(true);
        // 对于刘海屏机器如果webview被遮挡会自动padding

        IX5WebSettingsExtension settingsExtension = mWebView.getSettingsExtension();
        if (settingsExtension != null) {
            settingsExtension.setDisplayCutoutEnable(true);
        }

        if (!TextUtils.isEmpty(url) && (url.endsWith(".jpg") || url.endsWith(".png"))) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("<img  src=");
            stringBuilder.append(url);
            stringBuilder.append(">");
            mWebView.loadDataWithBaseURL(null, stringBuilder.toString(), "text/html", "charset=UTF-8", null);
            return;
        }

        mWebView.loadUrl(url);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_webview;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.stopLoading();
            mWebView.setWebViewClient(null);
            mWebView.destroy();
        }
    }
}
